package cn.kgc.tangcco.controller;

import cn.hutool.core.util.IdUtil;
import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.model.ResultCode;
import cn.kgc.tangcco.pojo.Account;
import cn.kgc.tangcco.utils.phone.BasePhone;
import cn.kgc.tangcco.utils.phone.SMS;
import cn.kgc.tangcco.vo.Login;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/28 23:21
 */
@RestController
@RequestMapping(value = "/account")
public class AccountController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 发送验证码
     *
     * @param mobile 手机号
     * @return
     */
    @GetMapping(value = "/sendCode")
    public ResponseText<SMS> sendCode(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        if (stringRedisTemplate.hasKey(mobile)) {
            return new ResponseText<>(ResultCode.NO_TIMEOUT.getCode(), ResultCode.NO_TIMEOUT.getMsg(), null);
        }
        SMS sms = BasePhone.phoneCode(mobile);
        if (sms == null) {
            return new ResponseText<>(ResultCode.UNKNOWN_HOST.getCode(), ResultCode.UNKNOWN_HOST.getMsg(), null);
        }
        if (!StringUtils.isBlank(sms.getOrder_id()) && "00000".equals(sms.getReturn_code())) {
            stringRedisTemplate.opsForValue().set(mobile, sms.getCode(), 60, TimeUnit.SECONDS);
        }
        return new ResponseText<SMS>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), sms);
    }

    @PostMapping(value = "/login")
    public ResponseText<Map<String, Object>> login(@RequestParam(value = "mobile", required = false) String mobile,
                                                         @RequestParam(value = "code", required = false) String code) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isBlank(mobile) || StringUtils.isBlank(code)) {
            return new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), map);
        }
        if (stringRedisTemplate.hasKey(mobile)) {
            String codeCache = stringRedisTemplate.opsForValue().get(mobile);
            if (code.equals(codeCache)) {
                Account account = new Account(mobile, "李昊哲", 95L);
                map.put("account", account);
                String token = IdUtil.fastSimpleUUID();
                // 存储鉴权token
                map.put("Authentication", token);
                stringRedisTemplate.opsForValue().set(token, JSON.toJSONString(account), 5, TimeUnit.MINUTES);
                return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), map);
            } else {
                return new ResponseText<>(ResultCode.CODE_ERROR.getCode(), ResultCode.CODE_ERROR.getMsg(), map);
            }
        }
        return new ResponseText<>(ResultCode.TIMEOUT.getCode(), ResultCode.TIMEOUT.getMsg(), map);
    }

    @GetMapping(value = "/query")
    public ResponseText<Account> query(@RequestHeader(value = "Authentication", required = false) String token) {
        String text = stringRedisTemplate.opsForValue().get(token);
        return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), JSON.parseObject(text, Account.class));
    }
}
